using System;
using System.Collections;
using UnityEngine;

public class GamePlayerprefs : MonoBehaviour
{
	public static GamePlayerprefs instance;

	[HideInInspector]
	public int allmoney;

	[HideInInspector]
	public int alldiamond;

	[HideInInspector]
	public int allexp;

	[HideInInspector]
	public int allenergy;

	[HideInInspector]
	public int maxenergy;

	[HideInInspector]
	public int showrate;

	[HideInInspector]
	public int levelfirst_win;

	[HideInInspector]
	public int levelfirst_lose;

	[HideInInspector]
	public int falsefirst;

	[HideInInspector]
	public int saleid;

	[HideInInspector]
	public string SaleStartTime;

	public int[] piecenum;

	public int[] ds_item_achi;

	public int[] daytask_item;

	public int[] surbestwave;

	private int[] playerExp = new int[18]
	{
		160,
		307,
		1010,
		1502,
		1983,
		3866,
		4892,
		5882,
		6837,
		10549,
		15364,
		13926,
		15291,
		16573,
		22399,
		19154,
		20167,
		21095
	};

	private string[] rankname = new string[19]
	{
		"列兵",
		"二等兵",
		"一等兵",
		"上等兵",
		"中士",
		"上士",
		"三级军士长",
		"一级军士长",
		"少尉",
		"中尉",
		"上尉",
		"少校",
		"中校",
		"上校",
		"准将",
		"少将",
		"中将",
		"上将",
		"五星上将"
	};

	public string[] ranknameImage = new string[19]
	{
		"Airman Basic",
		"Airman",
		"Airman First Class",
		"Senior Airman",
		"Staff Sergeant",
		"Technical Sergeant",
		"Master Sergeant",
		"Chief Master Sergeant",
		"Second Lieutenant",
		"First Lieutenant",
		"Captain",
		"Major",
		"Lieutenant Colonel",
		"Colonel",
		"Brigadier General",
		"Major General",
		"Lieutenant General",
		"General",
		"General of the Air Force"
	};

	private bool bestwaveflag;

	private bool readlandachi;

	private bool pieceflag;

	private bool islostfocus;

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
	}

	private void Start()
	{
		piecenum = new int[10];
		ds_item_achi = new int[18];
		daytask_item = new int[7];
		surbestwave = new int[3];
		StartCoroutine(FirstRead());
	}

	private IEnumerator FirstRead()
	{
		for (float i = 0f; i < 1f; i += Time.deltaTime)
		{
			yield return 0;
		}
		allmoney = PlayerPrefs.GetInt("coin", 200);
		alldiamond = PlayerPrefs.GetInt("diamond", 20);
		allenergy = PlayerPrefs.GetInt("energy", 30);
		allexp = PlayerPrefs.GetInt("exp", 0);
		maxenergy = PlayerPrefs.GetInt("maxenergy", 30);
		showrate = PlayerPrefs.GetInt("showrate", 0);
		daytask_item[6] = PlayerPrefs.GetInt("daytask" + 6);
		saleid = PlayerPrefs.GetInt("saleid", 0);
		SaleStartTime = PlayerPrefs.GetString("salestarttime", "1");
		if (GlobalInf.gamemode == TASKMODE.level)
		{
			levelfirst_win = getlevel_first_win(GlobalInf.CurrentLevel);
			levelfirst_lose = getlevel_first_lose(GlobalInf.CurrentLevel);
			falsefirst = PlayerPrefs.GetInt("falsefirst", 1);
		}
	}

	public void setmenufirst()
	{
		falsefirst = 0;
		PlayerPrefs.SetInt("falsefirst", 0);
		PlayerPrefs.SetInt("menufirst", 2);
	}

	public void save_sale(int id)
	{
		saleid = id;
		SaleStartTime = DateTime.Now.Ticks.ToString();
		PlayerPrefs.GetInt("saleid", saleid);
		PlayerPrefs.GetString("salestarttime", SaleStartTime);
		PlayerPrefs.SetInt("popsaleflag", 1);
	}

	public void set_popsaleflag()
	{
		PlayerPrefs.SetInt("popsaleflag", 1);
	}

	public void setgamefirst()
	{
		PlayerPrefs.SetInt("gamefirst", 0);
	}

	public void setrate()
	{
		showrate = 1;
		PlayerPrefs.SetInt("showrate", 1);
	}

	public int readsurbestwave(int mode)
	{
		if (bestwaveflag)
		{
			return surbestwave[mode];
		}
		bestwaveflag = true;
		surbestwave[mode] = PlayerPrefs.GetInt("surbestwave" + mode, 0);
		return surbestwave[mode];
	}

	public void setsurbestwave(int mode, int wave)
	{
		if (surbestwave[mode] < wave)
		{
			surbestwave[mode] = wave;
			PlayerPrefs.SetInt("surbestwave" + mode, wave);
		}
	}

	public int GetDelEnergy()
	{
		return maxenergy - allenergy;
	}

	public void readdailytask()
	{
		for (int i = 0; i < daytask_item.Length - 1; i++)
		{
			daytask_item[i] = PlayerPrefs.GetInt("daytask" + i, 0);
		}
	}

	public void savedailytask(int index, int num = 1)
	{
		if (daytask_item[index] != -1)
		{
			daytask_item[index] += num;
			PlayerPrefs.SetInt("daytask" + index, daytask_item[index]);
		}
	}

	public void adddailytask(int index, int num)
	{
		if (num != 0 && daytask_item[index] != -1)
		{
			daytask_item[index] += num;
			PlayerPrefs.SetInt("daytask" + index, daytask_item[index]);
		}
	}

	public void readachievement()
	{
		for (int i = 0; i < ds_item_achi.Length - 1; i++)
		{
			ds_item_achi[i] = PlayerPrefs.GetInt("item_achi" + i, 0);
		}
		if (ds_item_achi[2] == 0)
		{
			ds_item_achi[2] = 1;
		}
		if (ds_item_achi[3] == 0)
		{
			ds_item_achi[3] = 1;
		}
		if (ds_item_achi[4] == 0)
		{
			ds_item_achi[4] = 1;
		}
	}

	public void additemachi(int index, int num)
	{
		ds_item_achi[index] += num;
		PlayerPrefs.SetInt("item_achi" + index, ds_item_achi[index]);
	}

	public void saveitemachi(int index, int num)
	{
		if (ds_item_achi[index] < num)
		{
			ds_item_achi[index] = num;
			PlayerPrefs.SetInt("item_achi" + index, ds_item_achi[index]);
		}
	}

	public void saveFinishlevel(int curl)
	{
		if (ds_item_achi[0] < curl / 6)
		{
			ds_item_achi[0] = curl / 6;
			ds_item_achi[1] = curl / 6;
			PlayerPrefs.SetInt("item_achi0", ds_item_achi[0]);
			PlayerPrefs.SetInt("item_achi1", ds_item_achi[1]);
		}
	}

	public void saveLandachi()
	{
		if (!readlandachi)
		{
			ds_item_achi[17] = PlayerPrefs.GetInt("item_achi" + 17, 0);
			readlandachi = true;
		}
		ds_item_achi[17]++;
		PlayerPrefs.SetInt("item_achi17", ds_item_achi[17]);
	}

	public void savefightinfo()
	{
		FightInfo fightinfo = plane_smothfollow.instance.fightinfo;
		if (fightinfo.killnum_plane != 0)
		{
			additemachi(6, fightinfo.killnum_plane);
			savedailytask(5, fightinfo.killnum_plane);
		}
		if (fightinfo.killnum_ship != 0)
		{
			additemachi(7, fightinfo.killnum_ship);
		}
		if (fightinfo.killnum_build != 0)
		{
			additemachi(8, fightinfo.killnum_build);
		}
		if (fightinfo.diednum != 0)
		{
			additemachi(9, fightinfo.diednum);
		}
	}

	public void readpiece()
	{
		if (!pieceflag)
		{
			pieceflag = true;
			for (int i = 0; i < piecenum.Length; i++)
			{
				piecenum[i] = PlayerPrefs.GetInt("piecenum" + i, 0);
			}
		}
	}

	public void addpiece(int index, int num)
	{
		if (num != 0)
		{
			piecenum[index] += num;
			PlayerPrefs.SetInt("piecenum" + index, piecenum[index]);
		}
	}

	public bool DecEnergy(int num)
	{
		if (allenergy >= num)
		{
			allenergy -= num;
			PlayerPrefs.SetInt("energy", allenergy);
			return true;
		}
		return false;
	}

	public bool DecDiamond(int num)
	{
		if (alldiamond >= num)
		{
			alldiamond -= num;
			PlayerPrefs.SetInt("diamond", alldiamond);
			return true;
		}
		return false;
	}

	public void add_money(int num)
	{
		allmoney += num;
		PlayerPrefs.SetInt("coin", allmoney);
	}

	public void add_exp(int num)
	{
		allexp += num;
		PlayerPrefs.SetInt("exp", allexp);
	}

	public void add_diamond(int num)
	{
		alldiamond += num;
		PlayerPrefs.SetInt("diamond", alldiamond);
	}

	public void save_level_info(bool[] star)
	{
		int num = 0;
		int num2 = 0;
		int num3 = 1;
		for (int i = 0; i < 3; i++)
		{
			if (star[i])
			{
				num++;
				num2 += num3;
			}
			num3 *= 10;
		}
		int @int = PlayerPrefs.GetInt("curlevel");
		int int2 = PlayerPrefs.GetInt("biglevel" + GlobalInf.CurrentLevel / 6);
		if (GlobalInf.CurrentLevel == @int)
		{
			saveFinishlevel(@int);
		}
		int2 %= 1000000;
		int num4 = GlobalInf.CurrentLevel % 6;
		int num5 = 1;
		while (num4 > 0)
		{
			num4--;
			num5 *= 10;
		}
		if (int2 % (num5 * 10) < num * num5)
		{
			int2 = (int2 / (num5 * 10) * 10 + num) * num5 + int2 % num5;
			int2 += 1000000;
			if (GlobalInf.CurrentLevel == @int)
			{
				if (@int + 1 > 47)
				{
					PlayerPrefs.SetInt("curlevel", 47);
				}
				else
				{
					PlayerPrefs.SetInt("curlevel", @int + 1);
				}
				if (@int + 1 >= 17 && saleid == 1)
				{
					save_sale(2);
				}
				if ((@int + 1) % 6 == 0)
				{
					instance.set_popsaleflag();
				}
			}
			PlayerPrefs.SetInt("biglevel" + GlobalInf.CurrentLevel / 6, int2);
			PlayerPrefs.SetInt("levelstar" + GlobalInf.CurrentLevel, num2);
		}
		if (GlobalInf.CurrentLevel + 1 > 47)
		{
			PlayerPrefs.SetInt("nextlevel", 47);
		}
		else
		{
			PlayerPrefs.SetInt("nextlevel", GlobalInf.CurrentLevel + 1);
		}
	}

	public expdata GetExp(int expnum)
	{
		if (expnum < 0)
		{
			expnum = allexp;
		}
		expdata result = default(expdata);
		result.allexp = expnum;
		result.level = 0;
		int num = 0;
		for (int i = 0; i < 18; i++)
		{
			if (expnum - playerExp[i] >= 0)
			{
				expnum -= playerExp[i];
				num = i + 1;
			}
		}
		result.level = num + 1;
		result.name = rankname[num];
		result.curexp = expnum;
		if (num >= 18)
		{
			result.maxexp = 0;
			result.nextname = null;
		}
		else
		{
			result.maxexp = playerExp[num];
			result.nextname = rankname[num + 1];
		}
		return result;
	}

	public int getlevel_first_win(int lv)
	{
		return PlayerPrefs.GetInt("levelfirst_win" + lv, 1);
	}

	public void setlevel_first_win(int lv)
	{
		PlayerPrefs.SetInt("levelfirst_win" + lv, 0);
	}

	public int getlevel_first_lose(int lv)
	{
		return PlayerPrefs.GetInt("levelfirst_lose" + lv, 1);
	}

	public void setlevel_first_lose(int lv)
	{
		PlayerPrefs.SetInt("levelfirst_lose" + lv, 0);
	}

	private void OnDestroy()
	{
		if (instance != null)
		{
			instance = null;
		}
	}

	private void OnApplicationQuit()
	{
		adddailytask(6, (int)Time.time);
	}

	private void OnApplicationFocus(bool isfocus)
	{
		if (isfocus)
		{
			if (islostfocus)
			{
				Platform.cancleAllNotification();
			}
			return;
		}
		islostfocus = true;
		int delEnergy = GetDelEnergy();
		if (DateTime.Now.Hour < 19)
		{
			if ((19 - DateTime.Now.Hour) * 60 * 60 - DateTime.Now.Minute * 60 - DateTime.Now.Second >= delEnergy * 300 && delEnergy > 0)
			{
				Platform.setNotification(0, 1, 0L, Platform.notification[1]);
			}
			else
			{
				Platform.setNotification(0, 1, 0L, Platform.notification[0]);
			}
		}
		Platform.setNotification(1, 1, 0L, Platform.notification[2]);
		Platform.setNotification(2, 1, 0L, Platform.notification[2]);
		Platform.setNotification(3, 1, 0L, Platform.notification[1]);
		Platform.setNotification(4, 1, 0L, Platform.notification[3]);
		Platform.setNotification(5, 1, 0L, Platform.notification[3]);
		Platform.setNotification(6, 1, 0L, Platform.notification[3]);
	}
}
